#pragma once
#include<iostream>
using namespace std;
#include<functional>
#include<string>

class CalTask{
public:
    typedef function<int(int,int,char)> fun_t;
    CalTask(){

    }
    CalTask(int x,int y,char op,fun_t func):_x(x),_y(y),_op(op),_func(func){

    }

    //仿函数:返回值是string
    string operator()(){
        int ret = _func(_x,_y,_op);
        char buffer[1024];
        snprintf(buffer,sizeof buffer,"%d %c %d = %d",_x,_op,_y,ret);
        return buffer;
    }


    //给生产者使用的
    string toTaskString(){
        char buffer[1024];
        snprintf(buffer,sizeof buffer,"%d %c %d = ?",_x,_op,_y);
        return buffer;
    }

    ~CalTask(){

    }

private:
    int _x;
    int _y;
    char _op;//运算的符号
    fun_t _func;
};



class SaveTask{
    typedef function<void(const string&)> func_t;
public: 
    SaveTask(){

    }
    SaveTask(const string& message,func_t func):_message(message),_func(func){

    }

    void operator()(){
        _func(_message);//这里的_func就是保存到文件的操作，需要自己手动实现
    }

private:
    string _message;
    func_t _func;

};










const string oper = "+-*/%";
int mymath(int x,int y,char op){
    int result = 0;
    switch (op)
    {
    case '+':
        result = x + y;
    break;
    case '-':
        result = x - y;
    break;
    case '*':
        result = x * y;
    break;
    case '/':
    {
        if(y == 0){
            cerr<<"div zore error!"<<endl;
            result = -1;
        }else{
            result = x/y;
        }
    }
    break;
    case '%':
    {
        if(y == 0){
            cerr<<"div zore error!"<<endl;
            result = -1;
        }else{
            result =x % y;
        }
    }   
    break;
    
    
    default:
        break;
    }
    return result;
}



void Save(const string& message){
    const string target = "./log.txt";
    FILE* fp = fopen(target.c_str(),"a+");
    if(!fp){
        perror("fopen\n");
        return ;
    }
    fputs(message.c_str(),fp);
    fputs("\n",fp);
    fclose(fp);
}